import {api} from "../utils/api";

export const publicApiFn = {
    data() {
        return {
            popupObj: {
                show: false,
                actions: [],
                key: ""
            },
            ownerTypeCdList: [
              { id: "1001", name: "业主" },
              { id: "1002", name: "家庭成员" },
              { id: "1003", name: "房屋租客" }
            ]
        };
    },
    methods: {
        onSelect({mp}) {
            if(mp.detail.id == this.publicform[this.popupObj.key]) {
              return;
            }
            this.publicform[this.popupObj['key']] = mp.detail.id;
            this.publicform[this.popupObj['key']+'Name'] = mp.detail.name;
            switch (this.popupObj.key) { /**修改数据后相应关联的楼栋单元号需重置 */
              case 'communityId':
                this.publicform.floorId = '';
                this.publicform.floorIdName = '';
                this.publicform.unitId = '';
                this.publicform.unitIdName = '';
                this.publicform.roomId = '';
                this.publicform.roomIdName = '';
                this.publicform.ownerTypeCd = '1001';
                this.publicform.ownerTypeCdName = '业主';
                break;
              case 'floorId':
                this.publicform.unitId = '';
                this.publicform.unitIdName = '';
                this.publicform.roomId = '';
                this.publicform.roomIdName = '';
                this.publicform.ownerTypeCd = '1001';
                this.publicform.ownerTypeCdName = '业主';
                break;
              case 'unitId':
                this.publicform.roomId = '';
                this.publicform.roomIdName = '';
                this.publicform.ownerTypeCd = '1001';
                this.publicform.ownerTypeCdName = '业主';
                break;
            }
        },
        /**数据双向绑定 */
        handleClick({detail}, key) {
            this.popupObj['key'] = key;
            switch (key) {
            case 'communityId':
                this.allCommunityFn();
                break;
            case 'floorId':
                this.queryAllFloorsFn();
                break;
            case 'unitId':
                this.queryAllUnitsFn();
                break;
            case 'roomId':
                this.queryRoomsByUnitFn();
                break;
            case 'ownerTypeCd':
                this.queryOwnerTypeCdFn();
                break;
            }
        },
        /**获取小区列表 */
    async allCommunityFn() {
        const res = await api.allCommunity({});
        if(res.code == 0) {
          this.popupObj.actions = res.data;
        }
        if(!this.popupObj.actions.length) {
          wx.showToast({
            title: "暂无数据",
            icon: "none"
          })
          return;
        }
        this.popupObj.show = true;
      },
      /**获取楼栋列表 */
      async queryAllFloorsFn() {
        const res = await api.queryAllFloors({communityId: this.publicform.communityId});
        if(res.code == 0) {
          this.popupObj.actions = res.data;
        }
        if(!this.popupObj.actions.length) {
          wx.showToast({
            title: "暂无数据",
            icon: "none"
          })
          return;
        }
        this.popupObj.show = true;
      },
      /**获取单元列表 */
      async queryAllUnitsFn() {
        const res = await api.queryAllUnits({floorId: this.publicform.floorId});
        if(res.code == 0) {
          this.popupObj.actions = res.data;
        }
        if(!this.popupObj.actions.length) {
          wx.showToast({
            title: "暂无数据",
            icon: "none"
          })
          return;
        }
        this.popupObj.show = true;
      },
      /**获取房屋号列表 */
      async queryRoomsByUnitFn() {
        const res = await api.queryRoomsByUnit({unitId: this.publicform.unitId});
        if(res.code == 0) {
          this.popupObj.actions = res.data;
        }
        if(!this.popupObj.actions.length) {
          wx.showToast({
            title: "暂无数据",
            icon: "none"
          })
          return;
        }
        this.popupObj.show = true;
      },
      /**获取住户类型 */
      queryOwnerTypeCdFn() {
        this.popupObj.actions = this.ownerTypeCdList;
        this.popupObj.show = true;
      }
    }
}